package com.yugong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yugong.common.Const;
import com.yugong.entity.dto.Course;
import com.yugong.entity.vo.request.CourseRequestVO;
import com.yugong.entity.vo.response.CourseListResponseVO;
import com.yugong.mapper.CourseMapper;
import com.yugong.service.CourseService;
import com.yugong.util.TimeUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 课程业务逻辑实现类
 *
 * @author yugong
 * @date 2024/2/18
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private TimeUtils timeUtils;

    /**
     * 根据课程名查询课程信息
     *
     * @param pageNum    当前页
     * @param pageSize   当前页数据个数
     * @param courseName 课程名
     * @return 课程信息
     */
    @Override
    public PageInfo<CourseListResponseVO> findCourse(Integer pageNum, Integer pageSize, String courseName) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<Course> courseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseLambdaQueryWrapper
                .like(Course::getCourseName, courseName)
                .orderByDesc(Course::getCourseCreateTime);
        List<Course> courseList = courseMapper.selectList(courseLambdaQueryWrapper);
        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.like("course_name", courseName);
        Long courseCountNumber = courseMapper.selectCount(courseQueryWrapper);
        List<CourseListResponseVO> courseListResponseVOList = new ArrayList<>();
        for (Course course : courseList) {
            CourseListResponseVO courseListResponseVO = new CourseListResponseVO();
            courseListResponseVO.setCourseNo(course.getCourseNo());
            courseListResponseVO.setCourseName(course.getCourseName());
            courseListResponseVO.setCourseIntroduction(course.getCourseIntroduction());
            courseListResponseVO.setCourseTextbook(course.getCourseTextbook());
            courseListResponseVO.setCourseCreateMan(course.getCourseCreateMan());
            String createTime = timeUtils.setDateToStrDate(course.getCourseCreateTime(), Const.YYYY_MM_DD);
            courseListResponseVO.setCourseCreateTime(createTime);
            if (course.getCourseUpdateMan() == null) {
                courseListResponseVO.setCourseUpdateMan("未更新");
            } else {
                courseListResponseVO.setCourseUpdateMan(course.getCourseUpdateMan());
            }
            if (course.getCourseUpdateTime() == null) {
                courseListResponseVO.setCourseUpdateTime(null);
            } else {
                String updateTime = timeUtils.setDateToStrDate(course.getCourseUpdateTime(), Const.YYYY_MM_DD);
                courseListResponseVO.setCourseUpdateTime(updateTime);
            }
            courseListResponseVO.setCourseCountNumber(courseCountNumber);
            courseListResponseVOList.add(courseListResponseVO);
        }
        return PageInfo.of(courseListResponseVOList);
    }

    /**
     * 新增课程
     *
     * @param courseRequestVO 课程请求实体类
     * @return 数据库影响行数
     */
    @Override
    public int addCourse(CourseRequestVO courseRequestVO) {
        Course course = new Course();
        course.setCourseNo(courseRequestVO.getCourseNo());
        course.setCourseName(courseRequestVO.getCourseName());
        course.setCourseIntroduction(courseRequestVO.getCourseIntroduction());
        course.setCourseTextbook(courseRequestVO.getCourseTextbook());
        course.setCourseCreateMan(courseRequestVO.getCourseCreateMan());
        course.setCourseCreateTime(new Date());
        return courseMapper.insert(course);
    }

    /**
     * 修改课程
     *
     * @param courseRequestVO 课程请求实体类
     * @return 数据库影响行数
     */
    @Override
    public int updateCourseByCourseNo(CourseRequestVO courseRequestVO) {
        Course course = new Course();
        course.setCourseNo(courseRequestVO.getCourseNo());
        course.setCourseName(courseRequestVO.getCourseName());
        course.setCourseIntroduction(courseRequestVO.getCourseIntroduction());
        course.setCourseTextbook(courseRequestVO.getCourseTextbook());
        course.setCourseUpdateMan(courseRequestVO.getCourseUpdateMan());
        course.setCourseUpdateTime(new Date());
        return courseMapper.updateById(course);
    }

    /**
     * 删除课程
     *
     * @param courseNo 课程号
     * @return 数据库影响行数
     */
    @Override
    public int deleteCourseByCourseNo(String courseNo) {
        return courseMapper.deleteById(courseNo);
    }

    /**
     * 查询课程
     *
     * @return 课程
     */
    @Override
    public List<CourseListResponseVO> findCourse() {
        LambdaQueryWrapper<Course> courseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Course> courseList = courseMapper.selectList(courseLambdaQueryWrapper);
        List<CourseListResponseVO> courseListResponseVOList = new ArrayList<>();
        for (Course course : courseList) {
            CourseListResponseVO courseListResponseVO = new CourseListResponseVO();
            courseListResponseVO.setCourseNo(course.getCourseNo());
            courseListResponseVO.setCourseName(course.getCourseName());
            courseListResponseVO.setCourseIntroduction(course.getCourseIntroduction());
            courseListResponseVO.setCourseTextbook(course.getCourseTextbook());
            courseListResponseVO.setCourseCreateMan(course.getCourseCreateMan());
            String createTime = timeUtils.setDateToStrDate(course.getCourseCreateTime(), Const.YYYY_MM_DD);
            courseListResponseVO.setCourseCreateTime(createTime);
            if (course.getCourseUpdateMan() == null) {
                courseListResponseVO.setCourseUpdateMan("未更新");
            } else {
                courseListResponseVO.setCourseUpdateMan(course.getCourseUpdateMan());
            }
            if (course.getCourseUpdateTime() == null) {
                courseListResponseVO.setCourseUpdateTime(null);
            } else {
                String updateTime = timeUtils.setDateToStrDate(course.getCourseUpdateTime(), Const.YYYY_MM_DD);
                courseListResponseVO.setCourseUpdateTime(updateTime);
            }
            courseListResponseVOList.add(courseListResponseVO);
        }
        return courseListResponseVOList;
    }
}
